<template>
  <section class="loginContainer">
    <div class="loginInner">
      <div class="login_header">
        <h2 class="login_logo">Superman</h2>
        <div class="login_header_title">
          <a href="javascript:;" :class="className?'on':''" @click="className = true">短信登录</a>
          <a href="javascript:;" :class="!className?'on':''" @click="className = false">密码登录</a>
        </div>
      </div>
      <div class="login_content">
        <form @submit.passive="login">
          <div :class="className?'on':''">
            <section class="login_message">
              <input type="tel" maxlength="11" v-model="phonelength" placeholder="手机号">
              <button class="get_verification" :disabled="disa" @click.prevent="obtin">
                {{judge === true?'还有'+reciproca+'秒':'获取验证码'}}
              </button>
            </section>
            <section class="login_verification">
              <input type="text" maxlength="8" placeholder="验证码" pattern="{6}[0-9]" v-model="verification">
            </section>
            <section class="login_hint">
              温馨提示：未注册送餐超人帐号的手机号，登录时将自动注册，且代表已同意
              <a href="javascript:;">《用户服务协议》</a>
            </section>
          </div>
          <div :class="!className?'on':''">
            <section>
              <section class="login_message">
                <input type="text" maxlength="11" placeholder="手机/邮箱/用户名" v-model="username">
              </section>
              <section class="login_verification">
                <input type="password" maxlength="8" placeholder="密码" v-if="pwd" v-model="password">
                <input type="text" maxlength="8" placeholder="密码" v-else v-model="password">
                <div class="switch_button" :class="pwd?'off':'on'" @click="pwd = !pwd">
                  <div class="switch_circle" :class="pwd?'':'right'"></div>
                  <span class="switch_text">{{pwd?'显':'隐'}}</span>
                </div>
              </section>
              <section class="login_message">
                <input type="text" maxlength="11" placeholder="验证码" v-model="message">
                <img class="get_verification" :src="src" ref="verification" alt="captcha" @click="refresh">
              </section>
            </section>
          </div>
          <button class="login_submit" @click.prevent="login">登录</button>
        </form>
        <a href="javascript:;" class="about_us">关于我们</a>
      </div>
      <a href="javascript:" class="go_back" @click="$router.replace('/profile')">
        <i class="iconfont icon-fanhui"></i>
      </a>
    </div>
  </section>
</template>

<script>
  import { Toast } from 'mint-ui'
  import {reqSendcode,reqPhone,reqPwdIndeof} from '../../../api/index.js'

  export default {
    data(){
      return {
        phonelength: '',   //手机号
        verification: '',  //手机验证码
        username: '',   //账号
        password: '',   //密码
        message: '',    //图片验证码
        disa: true,       //判断是否禁止点击
        className: true,  //判断是什么方式登陆
        reciproca: 30, // 倒数
        judge: false,  //判断是否点击了
        pwd: true,     //判断是否显示密码
        src: 'http://localhost:4000/captcha?time='+Date.now()
      }
    },
    watch:{//监视value的动作
      'phonelength'(){
        //如果value的长度===11和输入的格式正确的话，就可以发送请求
        this.phonelength.length === 11 && this.phonelength.match(/^[1][3,4,5,8][0-9]{8}[0-9]/g)?this.disa = false:this.disa = true;
      }
    },
    methods:{
      //错误提示
      tost(text){
        Toast({
          message: text,
          position: 'bottom',
          duration: 5000
        });
      },
      //登陆
      async login(){
        let result
        const {phonelength,verification,username,password,message,className} = this
        const matchPhone = phonelength.match(/^[1][3,4,5,8][0-9]{8}[0-9]$/g); //筛选是否符合手机号规则
        const matchVerification = verification.match(/^[0-9]{6}$/g);  //筛选是否符合手机验证码规则
        //执行手机登陆
        if(className){
          if(phonelength.length !== 11 || !matchPhone){
            this.tost('请输入正确的手机号!')
            return
          }else if(verification.length !== 6 || !matchVerification){
            this.tost('验证码格式错误!')
            return
          }else{
            //AJAX异步发送请求
            result = await reqPhone(phonelength,verification)
          }

        }else{//执行密码登陆
          if(username.length > 18 || username === ''){
            this.tost('账号格式错误!')
            return
          }else if(password.length > 18 || password === ''){
            this.tost('密码格式错误!')
            return
          }else if(message.length !== 4 && message.length > 6 || message === ''){
            this.tost('验证码错误!')
            return
          }else{
            const name = username,
                  pwd = password,
                  captcha = message
            //AJAX异步发送请求
            result = await reqPwdIndeof({name,pwd,captcha})
          }

        }
        //数据处理
        if(result.code === 0){
          const response = result.data;
          this.$store.dispatch('setLogin',response);    //将用户信息存到vuex上进行管理
          this.$router.back();
        }else{
          const msg = result.msg;
          this.tost(msg);
          this.refresh()
        }
      },
      //发送验证码
      async obtin(){
        if(this.reciproca === 30 && this.disa !== true){ //判断定时器是否有启动
          const intercal = setInterval(() => {
            this.reciproca = this.reciproca - 1 //每秒减1
            this.judge = true   //事件点击了，已经触发obtin函数，赋值为true
            this.disa = false   //value的条件都满足，解除disabled属性，可以进行发送请求

            if(this.reciproca <= 0){  //减到零结束
              clearInterval(intercal)
              this.judge = false  //初始化事件点击
              this.reciproca = 30  //初始化秒数
            }
          },1000)
          const result = await reqSendcode(this.phonelength);
          if(result.code !== 0){
            this.tost(result.msg)
            clearInterval(intercal)
            this.judge = false  //初始化事件点击
            this.reciproca = 30  //初始化秒数
          }
        }
      },
      //更新验证码
      refresh(){
        this.src = 'http://localhost:4000/captcha?time='+Date.now()
      },
    }
  }
</script>

<style lang="stylus" rel="stylesheet/stylus" scoped>
  .loginContainer
    width 100%
    height 100%
    background #fff
    .loginInner
      padding-top 2.25rem
      width 80%
      margin 0 auto
      .login_header
        .login_logo
          font-size 1.5rem
          font-weight bold
          color #02a774
          text-align center
        .login_header_title
          padding-top 1.5rem
          text-align center
          >a
            color #333
            font-size .525rem
            padding-bottom .15rem
            &:first-child
              margin-right 1.5rem
            &.on
              color #02a774
              font-weight 700
              border-bottom .075rem solid #02a774
      .login_content
        >form
          >div
            display none
            &.on
              display block
            input
              width 100%
              height 100%
              padding-left .375rem
              box-sizing border-box
              border 1px solid #ddd
              border-radius .15rem
              outline 0
              font 400 .525rem Arial
              &:focus
                border .0375rem solid #02a774
            .login_message
              position relative
              margin-top .6rem
              height 1.8rem
              font-size .525rem
              background #fff
              .get_verification
                position absolute
                top 50%
                right .375rem
                transform translateY(-50%)
                border 0
                color #887E7E
                font-size .525rem
                background transparent
            .login_verification
              position relative
              margin-top .6rem
              height 1.8rem
              font-size .525rem
              background #fff
              .switch_button
                font-size .45rem
                border 1px solid #ddd
                border-radius .3rem
                transition background-color .3s,border-color .3s
                padding 0 .225rem
                width 1.125rem
                height .6rem
                line-height .6rem
                color #fff
                position absolute
                top 50%
                right .375rem
                transform translateY(-50%)
                .right
                  transform translateX(.75rem)
                &.off
                  background #fff
                  .switch_text
                    float right
                    color #ddd
                &.on
                  background #02a774
                >.switch_circle
                  //transform translateX(27px)
                  position absolute
                  top -1px
                  left -1px
                  width 16px
                  height 16px
                  border 1px solid #ddd
                  border-radius 50%
                  background #fff
                  box-shadow 0 .075rem .15rem 0 rgba(0,0,0,.1)
                  transition transform .3s
            .login_hint
              margin-top .412rem
              color #999
              font-size .525rem
              line-height .75rem
              >a
                color #02a774
          .login_submit
            display block
            width 100%
            height 1.575rem
            margin-top 1.125rem
            border-radius .15rem
            background #4cd96f
            color #fff
            text-align center
            font-size .6rem
            border 0
            border-radius .375rem
        .about_us
          display block
          font-size .45rem
          margin-top .75rem
          text-align center
          color #999
      .go_back
        position absolute
        top .188rem
        left .188rem
        width 1.125rem
        height 1.125rem
        >.iconfont
          font-size 1.5rem
          color #999
</style>
